package hu.elte.inf.holiuml.gui;

import hu.elte.inf.holiuml.model.ClassNode;
import hu.elte.inf.holiuml.model.Relation;
import hu.elte.inf.holiuml.model.RelationEdge;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Vector;

/**
 * @author Istvan Bozo
 */
public class RelationEdgeOptionsDialog extends OptionsDialog {

	private JPanel propPanel;
	JComboBox node1Combo;
	JComboBox node2Combo;
	JTextField connNameText;
	JComboBox connTypeComboBox;
	JTextField mult1TextField;
	JTextField mult2TextField;
	ClassNode beginNode;
	ClassNode endNode;
	JTextField firstRole;
	JTextField secondRole;
	Vector<String> classNames;
	Vector<ClassNode> classNodes;
	RelationEdge relationEdge;
	Vector<String> relationType = new Vector<String>();
	
	
	public RelationEdgeOptionsDialog(HoliUML parent, ClassNode beginNode, ClassNode endNode,
			Vector<ClassNode> classNodes) {
		super(parent, "Edge Options");
		this.beginNode = beginNode;
		this.endNode = endNode;
		classNames = new Vector<String>();
		setData(classNodes);
		setVisible(true);
	}
	
	//RelationEgde must be a non null object
	public RelationEdgeOptionsDialog(HoliUML parent, RelationEdge relationEdge, Vector<ClassNode> classNodes) {
		super(parent, "Edge Options");
		this.beginNode = (ClassNode)relationEdge.getNodeBegin();
		this.endNode = (ClassNode)relationEdge.getNodeEnd();
		this.relationEdge = relationEdge;
		classNames = new Vector<String>();
		setData(classNodes);
		fillInDatas();
		setVisible(true);
	}
	
	private void setData(Vector<ClassNode> classNodes){
		Relation relation = new Relation(null, null);
		Vector<String> types = relation.getTypes();
		for(int i = 0; i < types.size(); i++){
			relationType.add(types.get(i));
		}
		this.classNodes = new Vector<ClassNode>(classNodes);
		setNames();
		createPropPanel();
		fillPanels();
		setSize(new Dimension(450, 300));
		setModalityType(ModalityType.APPLICATION_MODAL);
		
	}
	private void fillInDatas(){
		connNameText.setText(relationEdge.getRelation().getName());
		firstRole.setText(relationEdge.getRelation().getRoleBegin());
		secondRole.setText(relationEdge.getRelation().getRoleEnd());
		for(int i = 0; i < relationType.size(); i++){
			if(relationEdge.getRelation().getType().toString().equals(relationType.get(i))){
				connTypeComboBox.setSelectedIndex(i);
			}
		}
		mult1TextField.setText(relationEdge.getRelation().getMultiBegin());
		mult2TextField.setText(relationEdge.getRelation().getMultiEnd());
		
	}
	private void setNames() {
		for (int i = 0; i < classNodes.size(); i++) {
			classNames.add(new String(classNodes.get(i).getUMLClass().getName()));
		}
	}

	private void createPropPanel() {
		propPanel = new JPanel(new GridLayout(8, 2));
		node1Combo = new JComboBox(classNames);
		node1Combo.setEditable(false);
		setComboIndex(beginNode, node1Combo);
		node2Combo = new JComboBox(classNames);
		node2Combo.setEditable(false);
		setComboIndex(endNode, node2Combo);
		connNameText = new JTextField();
		// fill in types
		connTypeComboBox = new JComboBox(relationType);
		Vector<String> multiplicity = new Vector<String>();
		multiplicity.add(new String("Number"));
		multiplicity.add(new String("Number..Number"));
		mult1TextField = new JTextField("1");
		mult2TextField = new JTextField("1");
		firstRole = new JTextField();
		secondRole = new JTextField();
		
		propPanel.add(new JLabel("Name of the first class:"));
		propPanel.add(node1Combo);
		propPanel.add(new JLabel("Name of the second class:"));
		propPanel.add(node2Combo);
		propPanel.add(new JLabel("Name of the connection:"));
		propPanel.add(connNameText);
		propPanel.add(new JLabel("Role of the first class:"));
		propPanel.add(firstRole);
		propPanel.add(new JLabel("Role of the second class:"));
		propPanel.add(secondRole);
		propPanel.add(new JLabel("Type of the connection:"));
		propPanel.add(connTypeComboBox);
		propPanel.add(new JLabel("Multiplicity of the first:"));
		propPanel.add(mult1TextField);
		propPanel.add(new JLabel("Multiplicity of the second:"));
		propPanel.add(mult2TextField);
	}
	
	private void setComboIndex(ClassNode classNode, JComboBox jComboBox){
		int index = classNodes.indexOf(classNode);
		jComboBox.setSelectedIndex(index);
	}

	private void fillPanels() {
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add("North", propPanel);
		getContentPane().add("South", buttonPanel);
	}

	public RelationEdge createRelation() {
		beginNode = classNodes.get(node1Combo.getSelectedIndex());
		endNode = classNodes.get(node2Combo.getSelectedIndex());
		Relation relation = new Relation(beginNode.getUMLClass(), 
										 endNode.getUMLClass());
		relation.setName(connNameText.getText());
		relation.setRelationType(connTypeComboBox.getSelectedItem().toString());
		relation.setMultiBegin(mult1TextField.getText());
		relation.setMultiEnd(mult2TextField.getText());
		relation.setRoleBegin(firstRole.getText());
		relation.setRoleEnd(secondRole.getText());
		RelationEdge relationEdge = new RelationEdge();
		relationEdge.setRelation(relation);
		relationEdge.setNodeBegin(beginNode);
		relationEdge.setNodeEnd(endNode);
		return relationEdge;
	}
	public void modifyData(RelationEdge relationEdge){
		relationEdge.setNodeBegin(classNodes.get(node1Combo.getSelectedIndex()));
		relationEdge.setNodeEnd(classNodes.get(node2Combo.getSelectedIndex()));
		relationEdge.getRelation().setRelationBegin(classNodes.get(node1Combo.getSelectedIndex()).getUMLClass());
		relationEdge.getRelation().setRelationBegin(classNodes.get(node1Combo.getSelectedIndex()).getUMLClass());
		relationEdge.getRelation().setRelationType(connTypeComboBox.getSelectedItem().toString());
		relationEdge.getRelation().setName(connNameText.getText());
		relationEdge.getRelation().setMultiBegin(mult1TextField.getText());
		relationEdge.getRelation().setMultiEnd(mult2TextField.getText());
		relationEdge.getRelation().setRoleBegin(firstRole.getText());
		relationEdge.getRelation().setRoleEnd(secondRole.getText());
	}

	protected boolean processOK() {
		return true;
	}

	protected void processCancel() {
	}
}
